let router = require('../router/Router');
let cmService = require('../service/cmService');
let agentService = require('../service/AgentService');
let userService = require('../service/userService');
let payAcctService = require('../service/payAcctService');

const { BusiError, SysError } = require('../exception/exception');

/**
 * 本周业绩查询
 *
 */
router.post("/agent/auth/perf/curweek", (req, resp) => {
    cmService.perfQuery(req.uid).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("err:", err);
        resp.send({code: 500, msg: err.message});
    });
});

/**
 * 本周直属下属查询
 * page
 */
router.post("/agent/auth/perf/curweek/du", (req, resp) => {
    if (!req.body.page) {
        req.body.page = 1;
    }
    let page = parseInt(req.body.page);
    if (page <= 0) page = 1;

    cmService.duPerfQuery(req.uid, page).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("err:", err);
        resp.send({code: 500, msg: err.message});
    });
});

/**
 * 团队管理
 */
router.post("/agent/auth/group", (req, resp) => {
    cmService.groupQuery(req.uid).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("err:", err);
        resp.send({code: 500, msg: err.message});
    });
});

/**
 * 团队管理
 */
router.post("/auth/group", (req, resp) => {
    cmService.groupQuery(req.uid).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("err:", err);
        resp.send({code: 500, msg: err.message});
    });
});

/**
 * 查询上级代理
 */
router.post("/agent/auth/group/parent", (req, resp) => {
    if (!req.body.page) {
        req.body.page = 1;
    }

    let page = parseInt(req.body.page);
    if (page <= 0) page = 1;

    let cond = {
        page: page,
        uid: req.uid
    };

    if (req.body.name) {
        cond.name = req.body.name;
    }

    if (req.body.acctId) {
        cond.acctId = req.body.acctId;
    }

    if (req.body.phone) {
        cond.phone = req.body.phone;
    }

    cmService.groupParentQuery(cond).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("group parent query err:", err);
        resp.send({code: 500, msg: "group parent query err"});
    });
});

/**
 * 查询下级
 */
router.post("/agent/auth/group/child", (req, resp) => {
    let page = parseInt(req.body.page);
    if (page <= 0) page = 1;

    let cond = {
        page: page,
        uid: req.uid
    };

    if (req.body.name) {
        cond.name = req.body.name;
    }

    if (req.body.phone) {
        cond.phone = req.body.phone;
    }

    if (req.body.acctId) {
        cond.acctId = req.body.acctId;
    }

    if (req.body.pAcctId){
        cond.pAcctId = req.body.pAcctId;
    }

    if (!req.body.pAcctId || req.body.pAcctId === "") {
        cond.pAcctId = req.uid;
    }

    cmService.groupChildQuery(cond).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("group child query err:", err);
        resp.send({code: 500, msg: "group child query err"});
    });
});

/**
 * 收益查询
 */
router.post("/agent/auth/prof", (req, resp) => {
    cmService.profQuery(req.uid).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("prof query err:", err);
        resp.send({code: 500, msg: err.message});
    });
});

router.post("/agent/auth/acct/info", (req, resp) => {
    cmService.getAcctInfo(req.uid).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("get acct info err:", err);
        resp.send({code: 500, msg: err.message});
    });
});

/**
 * 收益提现查询
 *  @param Aid       *int64 `form:"aid"`
 AcctId    string `form:"acct_id"`
 TypeCode  string `form:"type_code"`
 FlowNo    string `form:"flow_no"`
 Status    *int64 `form:"status" validate:"exists"`
 StartTime string `form:"start_time"`
 EndTime   string `form:"end_time"`
 Page      *int64 `form:"page" validate:"exists"`
 */
router.post("/agent/auth/prof/withdraw/query", (req, resp) => {
    if (!req.body.page) {
        req.body.page = 1;
    }
    let page = parseInt(req.body.page);
    if (page <= 0) page = 1;
    let cond = {
        page: page,
        acctId: req.uid,
        uid: req.uid
    };

    if (req.body.status != null && req.body.status !== undefined) {
        cond.status = parseInt(req.body.status);
    }

    if (req.body.flowNo) {
        cond.flowNo = req.body.flowNo;
    }

    if (req.body.startTime) {
        cond.startTime = req.body.startTime;
    }

    if (req.body.endTime) {
        cond.endTime = req.body.endTime;
    }

    if (req.body.cardNo){
        cond.cardNo = req.body.cardNo;
    }

    cmService.wdQuery(cond).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("withdraw query err:", err);
        resp.send({code: 500, msg: err.message});
    });
});

/**
 * 收益提现
 * @param   AcctId   string  `form:"acctId" binding:"required"`
            TypeCode string  `form:"typeCode" binding:"required"`
            Amt      float64 `form:"amt" binding:"required"`
            Type     string  `form:"type" binding:"required"`
            CardNo   string  `form:"cardNo" binding:"required"`
            FeeRate  float64 `form:"feeRate" binding:"required"`
 */
router.post("/agent/auth/prof/withdraw", (req, resp) => {
    let cond = {
        uid: req.uid
    };
    if (!req.body.amt) {
        console.logw("withdraw err: 提现金额不能为空");
        resp.send({code: 500, msg: "提现金额不能为空"});
    }
    cond.amt = req.body.amt;

    if (!req.body.type) {
        console.logw("withdraw err: 提现类型不能为空");
        resp.send({code: 500, msg: "提现类型不能为空"});
    }
    cond.type = req.body.type;

    cmService.wd(cond).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        console.logw("withdraw err:", err);
        resp.send({code: 500, msg: err.message});
    });
});

/**
 * 用户登录接口
 */
router.post("/agent/user/login", function (req, resp) {
    let phone = req.body.acct;
    let password = req.body.pwd;
    // if (!(/^1(3|4|5|7|8)\d{9}$/.test(phone))) {
    //     console.logg("手机号码格式不对 ", phone);
    //     resp.send({code: 500, msg: '手机号码格式不对'});
    //     return;
    // }

    if (password && password.length < 8) {
        resp.send({code: 500, msg: '请输入8位以上的密码'});
        return;
    }

    agentService.validAgentUser(phone, password).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(err => {
        let msg = "系统异常, 请稍后再试";
        if (err instanceof BusiError){
            msg = err.message;
        }
        resp.send({code: 500, msg: msg});
    });
});

router.post("/agent/auth/pay/acct", function (req, resp) {
    payAcctService.getAcct(req.uid).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(e => {
        resp.send({code: 500, msg: e});
    });
});

router.post("/agent/auth/pay/acct/alter/alipay", function (req, resp) {
    payAcctService.alter({uid: req.uid, acct: req.body.acct, realName: req.body.realName, type: 'A0000'}).then(res => {
        if (res > 0) {
            resp.send({code: 200, msg: "success", data: req.body.acct});
        } else {
            resp.send({code: 500, msg: "操作失败"});
        }
    }).catch(e => {
        resp.send({code: 500, msg: e});
    });
});

router.post("/agent/auth/pay/acct/alter/bankCard", function (req, resp) {
    payAcctService.alter({uid: req.uid, acct: req.body.acct, realName: req.body.realName, type: 'B0000'}).then(res => {
        if (res > 0) {
            resp.send({code: 200, msg: "success", data: req.body.acct});
        } else {
            resp.send({code: 500, msg: "操作失败"});
        }
    }).catch(e => {
        resp.send({code: 500, msg: e});
    });
});

/**
 * agent token验证
 */
router.post("/agent/auth/token/valid", (req, resp) => {
    resp.send({code: 200, msg: "success"});
});

router.post('/agent/auth/promote/link', (req, resp) => {
    userService.getPromoteLink(req.uid).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(e => {
        let msg = "系统异常, 请稍后再试";
        if (e instanceof BusiError){
            msg = e.message;
        }

        resp.send({code: 500, msg: msg});
    });
});